package zzjson.rabbitmq.delay;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import zzjson.rabbitmq.BaseRabbitMq;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.rabbitmq.message</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年04月25日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class DelayPluginProducer {
	public static void main(String[] args) throws IOException {
		Connection conn = BaseRabbitMq.getConn();
		Channel channel = conn.createChannel();

		// 延时投递，比如延时1分钟
		Date now = new Date();
		Calendar delayTimeD = Calendar.getInstance();
		delayTimeD.add(Calendar.MINUTE, +1);// 1分钟后投递
		Date delayTime = delayTimeD.getTime();

		// 准时投递，把这个值替换delayTime即可
		Date exactDealyTime = new Date("2019/04/25,22:30:00");

		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		String msg = "publish at " + sf.format(now) + " \t deliver at " + sf.format(delayTime);

		// 延迟的间隔时间，目标时刻减去当前时刻
		Map<String, Object> headers = new HashMap<String, Object>();
		headers.put("x-delay", delayTime.getTime() - now.getTime());

		AMQP.BasicProperties.Builder props = new AMQP.BasicProperties.Builder()
				.headers(headers);
		channel.basicPublish("DELAY_EXCHANGE", "DELAY_KEY", props.build(),
				msg.getBytes());

		BaseRabbitMq.close(channel, conn);

	}
}